package com.yksj.healthtalk.ui.chatting;

import java.io.File;
import java.util.List;

import org.handmark.pulltorefresh.library.PullToRefreshBase;
import org.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import org.handmark.pulltorefresh.library.PullToRefreshListView;
import org.universalimageloader.utils.StorageUtils;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;

import com.yksj.healthtalk.adapter.DoctorAndPatientContentAdapter;
import com.yksj.healthtalk.comm.BaseFragmentActivity;
import com.yksj.healthtalk.comm.ImageGalleryActivity;
import com.yksj.healthtalk.entity.MessageEntity;
import com.yksj.healthtalk.net.http.HttpRestClient;
import com.yksj.healthtalk.net.http.ObjectHttpResponseHandler;
import com.yksj.healthtalk.net.http.RequestParams;
import com.yksj.healthtalk.net.socket.SmartFoxClient;
import com.yksj.healthtalk.ui.R;
import com.yksj.healthtalk.ui.chatting.ChatActivity.VoiceFileHttpResponseHandler;
import com.yksj.healthtalk.utils.DataParseUtil;
/**
 * 医患对话
 * @author jack_tang
 *
 */
public class DoctorAndPatientContentActivity extends BaseFragmentActivity implements OnClickListener, OnRefreshListener<ListView> {

	private ListView mListview;
	private String CHAT_GROUP_ID=String.valueOf(Long.MAX_VALUE);
	private DoctorAndPatientContentAdapter mAdapter;
	private PullToRefreshListView mPullToRefreshListView;
	private final String mUserId=SmartFoxClient.getLoginUserId();
	
	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.doctor_chatting_content_layout);
		initView();
	}

	/**
	 * 显示聊天大图片
	 * @param str
	 */
	public void onShowBigImage(String str){
		Intent intent = new Intent(this,ImageGalleryActivity.class);
		intent.putExtra(ImageGalleryActivity.URLS_KEY,new String[]{str});
		intent.putExtra(ImageGalleryActivity.TYPE_KEY,0);//0,1单个,多个
		intent.putExtra("type",1);
		startActivityForResult(intent,3000);
	}
	
	/**
	 * 语音播放
	 * @param entity
	 */
	public void onVoicePlay(MessageEntity entity){
//		if(StorageUtils.isSDMounted()){
//			mPlayingEntity = entity;
//			String path = entity.getContent();
//			if(path.contains("/")){//远程路径
//				String name = StorageUtils.getFileName(path);
//				File file = new File(StorageUtils.getVoicePath(),name);
//				if(file.exists()){//文件不存在需要去下载
//					mediaPlay.play(entity);
//				}else{
//					HttpRestClient.doHttpDownChatFile(path,new VoiceFileHttpResponseHandler(entity));
//				}
//			}else{
//				mediaPlay.play(entity);
//			}
//		}
	}
	
	
	/**
	 * 地图消息点击
	 * 
	 * @param messageEntity
	 */
	public void onLocationMesgClick(MessageEntity messageEntity) {
		
		Intent intent  = new Intent(this,ChatMapActivity.class);
		String [] str = messageEntity.getContent().split("&");
		intent.putExtra("lo", str[0]);
		intent.putExtra("la", str[1]);
		startActivity(intent);
	}
	private void initView() {
		initTitle();
		titleTextV.setText("医患对话");
		titleLeftBtn.setOnClickListener(this);
		mPullToRefreshListView = (PullToRefreshListView) findViewById(android.R.id.list);
		mAdapter = new DoctorAndPatientContentAdapter(this, true, null);
		mListview = mPullToRefreshListView.getRefreshableView();
		mListview.setAdapter(mAdapter);
		mPullToRefreshListView.setRefreshing();
		mPullToRefreshListView.setOnRefreshListener(this);
		CHAT_GROUP_ID=getIntent().getStringExtra("CHAT_GROUP_ID");
		initData();
	}

	private void initData() {
		RequestParams params=new RequestParams();
		params.put("Type","queryAskByChatId");
		params.put("CHAT_GROUP_ID", CHAT_GROUP_ID);
		HttpRestClient.doHttpNewsRequesDocotor(params, new ObjectHttpResponseHandler() {
			@Override
			public Object onParseResponse(String cotent) {
				List<MessageEntity> list = DataParseUtil.parseToGroupMessage(cotent,mUserId);
				return list;
			}
			@Override
			public void onSuccess(int statusCode, Object response) {
				super.onSuccess(statusCode, response);
				if(response != null)mAdapter.addCollectionToTop((List<MessageEntity>)response);
				CHAT_GROUP_ID=mAdapter.getFirstMesgId();
			}
			@Override
			public void onFinish() {
				super.onFinish();
				mPullToRefreshListView.onRefreshComplete();
			}
		});
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.title_back:
			onBackPressed();
			break;
		}
	}

	@Override
	public void onRefresh(PullToRefreshBase<ListView> refreshView) {
		mPullToRefreshListView.onRefreshComplete();
	}
	
}
